package com.study.controller;

import com.study.entity.User;
import com.study.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/10/18/018.
 */
@Controller
@RequestMapping("user")
public class UserController {
    private Logger logger=LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;

    @PostMapping("doLogin")
    public String doLogin(User user, HttpSession session, RedirectAttributes ra){
        User u = userService.findUser(user);
        logger.debug("当前登录用户={}",u);
        if (u == null) {
            ra.addAttribute("error","用户名或者密码错误");
            session.setAttribute("userName",user.getUserName());
            session.setAttribute("userPwd",user.getUserPwd());
            return "redirect:/login.jsp";//重定向
        }else{
            session.setAttribute("user",u);
            return "main";
        }
    }
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/login.jsp";
    }
}
